
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
 */
package itm.dao.administracion;

//~--- non-JDK imports --------------------------------------------------------

import itm.exception.ControlException;

import itm.vo.administracion.TipoAtributoVO;

//~--- JDK imports ------------------------------------------------------------

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author BISA20
 */
public class TipoAtributoDAO {
    private CallableStatement callStmt = null;
    private ResultSet         res      = null;
    private String            sql      = "";
    Connection                dbConexion;

    public TipoAtributoDAO(Connection dbConexion) {
        this.dbConexion = dbConexion;
    }

    public String crearTipoAtributo(TipoAtributoVO tipoAtributoVO) throws ControlException, SQLException {
        String temp = "";

        try {
            sql      = "{call ITM_CREAR_TIPO_ATRIBUTO(?,?)}";
            callStmt = dbConexion.prepareCall(sql);
            callStmt.registerOutParameter(1, java.sql.Types.VARCHAR);
            callStmt.setString(2, tipoAtributoVO.getNombreTipoAtributo());
            callStmt.execute();
            temp = callStmt.getString(1);

            String qq = temp;
        } catch (SQLException ce) {
            ce.getStackTrace();
        }

        return temp;
    }

    public List<TipoAtributoVO> consultarTipoAtributo(TipoAtributoVO tipoAtributoVO)
            throws ControlException, SQLException {
        List<TipoAtributoVO> listaTipoAtributo = new ArrayList<TipoAtributoVO>();

        try {
            sql      = "{call ITM_CONSULTAR_TIPO_ATRIBUTO(?)}";
            callStmt = dbConexion.prepareCall(sql);
            callStmt.setString(1, tipoAtributoVO.getNombreTipoAtributo());
            res = callStmt.executeQuery();

            while (res.next()) {
                tipoAtributoVO = new TipoAtributoVO();
                tipoAtributoVO.setNombreTipoAtributo(res.getString(1));
                tipoAtributoVO.setIdTipoAtributo(res.getString(2));

                // categoriaProductosVO.setIdCategoria(res.getString(2));
                listaTipoAtributo.add(tipoAtributoVO);
            }
        } catch (SQLException ce) {
            ce.getStackTrace();
        }

        return listaTipoAtributo;
    }

    public String eliminarTipoAtributo(TipoAtributoVO tipoAtributoVO) throws ControlException, SQLException {
        String temp = "";

        try {
            sql      = "{call ITM_ELIMINAR_TIPO_ATRIBUTO(?,?)}";
            callStmt = dbConexion.prepareCall(sql);
            callStmt = dbConexion.prepareCall(sql);
            callStmt.registerOutParameter(1, java.sql.Types.VARCHAR);
            callStmt.setString(2, tipoAtributoVO.getNombreTipoAtributo());
            callStmt.execute();
            temp = callStmt.getString(1);

            String qq = temp;
        } catch (SQLException ce) {
            ce.getStackTrace();
        }

        return temp;
    }

    public String actualizarTipoAtributo(TipoAtributoVO tipoAtributoVO) throws ControlException, SQLException {
        String temp = "";

        try {
            sql      = "{call ITM_ACTUALIZAR_TIPO_ATRIBUTO(?,?,?)}";
            callStmt = (CallableStatement) dbConexion.prepareCall(sql);
            callStmt.registerOutParameter(1, java.sql.Types.VARCHAR);
            callStmt.setString(2, tipoAtributoVO.getIdTipoAtributo());
            callStmt.setString(3, tipoAtributoVO.getNombreTipoAtributo());
            callStmt.execute();
            temp = callStmt.getString(1);

            String qq = temp;
        } catch (SQLException ce) {
            ce.getStackTrace();
        }

        return temp;
    }

    public List<TipoAtributoVO> listarTipoatributo() throws Exception {
        List<TipoAtributoVO> listaTipoAtributo = new ArrayList<TipoAtributoVO>();
        TipoAtributoVO       tipoAtributoVO;

        try {
            sql      = "{call ITM_LISTA_TIPO_ATRIBUTOS()}";
            callStmt = dbConexion.prepareCall(sql);
            res      = callStmt.executeQuery();

            while (res.next()) {
                tipoAtributoVO = new TipoAtributoVO();
                tipoAtributoVO.setIdTipoAtributo(res.getString(1));
                tipoAtributoVO.setNombreTipoAtributo(res.getString(2));
                listaTipoAtributo.add(tipoAtributoVO);
            }
        } catch (Exception ce) {
            ce.getStackTrace();
        }

        return listaTipoAtributo;
    }
}
